﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KingNET.Engine;
using KingNET.Enum;
using KingNET.Interface;

namespace KingNET.Controls
{

    class Card : Panel, ICard
    {

        #region BackColor
        /// <summary>
        /// Card backcolor
        /// </summary>
        public override Color BackColor
        {
            get
            {
                return Color.Transparent;
            }
            set
            {
                if (value.ToArgb() != Color.Transparent.ToArgb())
                    value = Color.Transparent;

                base.BackColor = Color.Transparent;
            }
        }
        #endregion

        #region MinimumSize

        private readonly Size minimumCardSize = new Size(79, 115);

        /// <summary>
        /// Minimum card size
        /// </summary>
        public override Size MinimumSize
        {
            get
            {
                return minimumCardSize;
            }
            set
            {
                if ((value.Height != minimumCardSize.Height && value.Width != minimumCardSize.Width)
                    || value.IsEmpty)
                    return;

                base.MinimumSize = value;
            }
        }
        #endregion

        #region Properties
        private readonly Suit suit = Suit.Spades;

        /// <summary>
        /// Suit
        /// </summary>
        public Suit Suit
        {
            get { return suit; }
        }

        private readonly CardValue cardValue = CardValue.CardAce;

        /// <summary>
        /// Card Value
        /// </summary>
        public CardValue CardValue
        {
            get { return cardValue; }
        }

        private static Image cardBackImage = null;
        /// <summary>
        /// Card Back Image
        /// </summary>
        public static Image CardBackImage
        {
            get
            {
                if (cardBackImage == null)
                    cardBackImage = Helper.GetCardImage(2, 4);

                return cardBackImage;
            }
        }

        private bool showCard = true;
        /// <summary>
        /// Show card front side
        /// </summary>
        public bool ShowCard
        {
            get { return showCard; }
            set
            {
                showCard = value;
                Invalidate();
            }
        }
        #endregion

        // Var

        private readonly Image image = null;

        #region Ctor

        /// <summary>
        /// Card
        /// </summary>
        /// <param name="tSuit"></param>
        /// <param name="tCardValue"></param>
        public Card(Suit tSuit, CardValue tCardValue)
        {

            //

            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            SetStyle(ControlStyles.AllPaintingInWmPaint
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.UserPaint, true);


            //

            suit = tSuit;
            cardValue = tCardValue;

            this.Size = minimumCardSize;

            if (image == null)
                image = Helper.GetCardImage((int)this.CardValue, (int)this.Suit);

            //

        }
        #endregion

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);

            if (image != null && this.ShowCard)
                pevent.Graphics.DrawImage(image, new Rectangle(new Point(0, 0), this.Size));

            if (Card.CardBackImage != null && !this.ShowCard)
                pevent.Graphics.DrawImage(Card.CardBackImage, new Rectangle(new Point(0, 0), this.Size));

        }


        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                this.ShowCard = !this.ShowCard;

        }

        public void ScaleCard(float scaleWidth)
        {
            SizeF size = new SizeF(this.MinimumSize.Width * scaleWidth, this.MinimumSize.Height * scaleWidth);
            this.Size = size.ToSize();
            this.Invalidate();
        }


    }
}
